package com.yunhui.controller;


import com.yunhui.entity.Traveltype;
import com.yunhui.service.TraveltypeService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 行程类型的前端控制层
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/traveltype")
public class TraveltypeController {


    //注入service层
    @Resource
    TraveltypeService traveltypeService;


    /**
     * 查询所有的父类或者子类的所有类型
     */
    @RequestMapping("finAll")
    @ResponseBody
    public Object finAll(String parentId) {
        return traveltypeService.getAppCategoryList(parentId);
    }

    /**
     * 跳转一级管理页面
     *
     * @return
     */
    @RequestMapping("goAddFuTpye")
    public String goadd(Model model) {
        //从业务层获取所有一级类型
        List<Traveltype> list = traveltypeService.getAppCategoryList(null);
        model.addAttribute("fulist", list);
        return "houtai/comment-list";
    }


    /**
     * 跳转二级管理页面
     *
     * @param model
     * @return
     */
    @RequestMapping("goSonType")
    public String goSonType(Model model) {
        List<Traveltype> sonList = traveltypeService.getTpyeSonLsit();
        List<Traveltype> fuList = traveltypeService.getAppCategoryList(null);
        model.addAttribute("sonList", sonList);
        model.addAttribute("baList", fuList);
        return "houtai/sonlist";
    }

    /**
     * 跳转商家页面
     *
     * @return
     */
    @RequestMapping("shangIndex")
    public String goShangjia() {
        return "shang/index";
    }

    @RequestMapping("goChan")
    public String goChan() {
        return "shang/product";
    }


    /**
     * 使用ajax实现添加操作
     *
     * @param traveltype
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Object doAdd(Traveltype traveltype) {
        traveltype.setCreateTime(new Date());
        if (traveltypeService.getTraveltypeByName(traveltype.getTypeName()) != null) {
            return "-2";
        }
        int num = traveltypeService.addTraveltype(traveltype);
        return num + "";
    }

    /**
     * 删除父级类型类型
     *
     * @return
     */
    @SuppressWarnings("null")
	@RequestMapping("deleteByid")
    @ResponseBody
    public Object deleteById(String id) {
        List<Traveltype> list = traveltypeService.getAppCategoryList(id);
        if (list != null || list.size() > 0) {
            return "-2";
        }
        if (traveltypeService.delteTraveltypeById(Integer.parseInt(id))) {
            return "1";
        }
        return "0";
    }

    /**
     * 删除二级类型
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteSonByid")
    @ResponseBody
    public Object deleteSonById(String id) {
        int num = traveltypeService.selectRoutejbCountBySonid(id);
        if (num > 0) {
            return "-2";
        }
        if (traveltypeService.delteTraveltypeById(Integer.parseInt(id))) {
            return "1";
        }
        return "0";
    }


}

